import asyncio
import aiohttp


async def request(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            result = await response.text()
            return result


async def get_message():
    await asyncio.sleep(0.2)
    return "async message"


def coroutines_example():
    result = ""
    count = 0
    while True:
        value = yield result
        count += 1
        try:
            value = str(value)
            result += value + " "
            print(f"current result: {result} add count is :{count}")
        except ValueError:
            value = yield 0
            print(f"{value} type error, can't be transform string type!")


if __name__ == "__main__":
    my_coroutines = coroutines_example()
    print(next(my_coroutines))
    my_coroutines.send(1)
    my_coroutines.send("33")

    # loop = asyncio.get_event_loop()
    # task_arr = [get_message() for _ in range(100)]
    # result_arr = loop.run_until_complete(asyncio.gather(*task_arr))
    # print(result_arr)
    # loop.close()

    request_loop = asyncio.get_event_loop()
    urls = ["https://www.jd.com"] * 10
    task_arr = [request(url) for url in urls]
    results = request_loop.run_until_complete(asyncio.gather(*task_arr))
    print(results)
